package com.qiuzhiyi.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.li.widget.HackyViewPager;
import com.qiuzhiyi.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ImageListActivity extends AppCompatActivity {

    @BindView(R.id.pager)
    HackyViewPager mPager;
    @BindView(R.id.txt_page)
    TextView mTxtPage;

    private List<PhotoView> mImageViewList;
    private int mPos;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_image_list);
        ButterKnife.bind(this);


        mImageViewList=new ArrayList<>();

        mPos=getIntent().getIntExtra("1",0);

        for (String s : getIntent().getExtras().getStringArrayList("0")) {
            PhotoView imageView=new PhotoView(this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(params);
            Glide.with(this).load(s).into(imageView);

            mImageViewList.add(imageView);
        }
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                mTxtPage.setText(position+1+"/"+mImageViewList.size());
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mImageViewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mImageViewList.get(position));
                return mImageViewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mImageViewList.get(position));
            }
        });
        mPager.setCurrentItem(mPos);
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
